﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.Wap.Code
{
    //需要登录的页面继承该页面
    public class MemberPageBase : System.Web.UI.Page
    {
        public MemberPageBase()
        {
            NeedLogin = true;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            //Session[GlobalSessionString.CurrentMemberAccount] = "test";

            if (Session[GlobalSessionString.CurrentMemberAccount] != null && CurrentMember != null)
            //已登录
            {
                //项目的业务逻辑
                //注册完后需完善资料等操作
            }
            //未登录
            else
            {
                //当前用户是否存在(有可能被后台删了)
                if (CurrentMember == null)
                {
                    Session[GlobalSessionString.CurrentMemberAccount] = null;
                }

                //需要登录则跳转登录页面
                if (NeedLogin)
                {
                    //获取手机端默认登录地址
                    string loginUrl = ParamKit.GetValue("SystemConfig", "WapLogin", "/wap/member/login.aspx");

                    //从定向到登录页面
                    Response.Redirect(loginUrl + "?goto=" + Server.UrlEncode(Request.RawUrl) + "&origin=" + Request["origin"]);
                }

                //
                //用户通过以下方式获取自己的推广码
                //
                //ICommandResult<GetPromotionCodeResult> res = new GetPromotionCodeCommand().Execute(new GetPromotionCodeParameter() { MemberAccount = CurrentMemberAccount });
                //promotionCode = res.Result.PromotionCode;

                //
                //处理被推广，增长推广这的积分，积分按推广规则
                //
                //string promotionCode = Request["pcode"];
                //if (!string.IsNullOrEmpty(promotionCode) && !string.IsNullOrEmpty(CurrentMemberAccount))
                //{
                //    using (ISession session = SessionFactoryHelper.CreateSession())
                //    {
                //        if (Convert.ToInt32(session.CreateQuery("select count (*) from MemberPromotionLog where MemberAccount=:member")
                //            .SetParameter("member", CurrentMemberAccount)
                //            .UniqueResult()) == 0)
                //        {


                //            MemberPromotionInfo promitionInfo = session.CreateQuery("from MemberPromotionInfo where PromotionCode=:code")
                //           .SetParameter("code", promotionCode).UniqueResult<MemberPromotionInfo>();

                //            if (string.IsNullOrEmpty(CurrentMemberAccount) || promitionInfo == null)
                //            {
                //            }
                //            else
                //            {
                //                ICommandResult<PromotionCallbackResult> callbackResult = new PromotionCallbackCommand().Execute(new PromotionCallbackParameter()
                //                {
                //                    OwnerAccount = Owner,
                //                    MemberAccount = promitionInfo.MemberAccount
                //                });
                //                if (!callbackResult.Error)
                //                {
                //                    MemberPromotionLog log = new MemberPromotionLog();
                //                    log.CreateDate = DateTime.Now;
                //                    log.MemberAccount = CurrentMemberAccount;
                //                    log.PromotionId = promitionInfo.Id;
                //                    session.Save(log);
                //                }
                //            }
                //        }
                //    }
                //}
            }

        }


        public string Owner
        {
            get
            {
                return CurrentMember.OwnerAccount;
            }
        }

        /// <summary>
        /// 当前用户帐号
        /// </summary>
        public string CurrentMemberAccount
        {
            get
            {
                return Session[GlobalSessionString.CurrentMemberAccount] as string;
            }
            private set { Session[GlobalSessionString.CurrentMemberAccount] = value; }
        }

        public MemberInfo CurrentMember
        {
            get
            {
                if (_member == null)
                {
                    using (ISession session = SessionFactoryHelper.CreateSession())
                    {
                        _member = session.CreateQuery("from MemberInfo where AccountId=:acc")
                            .SetParameter("acc", CurrentMemberAccount)
                            .UniqueResult<MemberInfo>();
                    }
                }
                return _member;
            }
        }


        public static String GetHeadPhoto(string url, string sex)
        {
            if (string.IsNullOrEmpty(url))
            {
                return "/Wap/images/nohead_" + sex + ".png";
            }
            else
            {
                return FileKit.FirstFilePath(url);
            }
        }


        public string CurrentMemberHeadPhoto
        {
            get
            {
                return GetHeadPhoto(CurrentMember.PhotoUrl, CurrentMember.Sex);
            }
        }

        /// <summary>
        /// 是否需要登录
        /// </summary>
        public bool NeedLogin { get; set; }


        protected MemberInfo _member = null;
    }
}